/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.additional;

import com.sun.lwuit.Label;
import core.DriverManager;

/**
 *
 * @author Kvest
 */
public class TimerLabel extends Label
{
    private static final int ZERO_TIME = DriverManager.TIMER_TIMEOUT;
    private static final int ZERO_COLOR = 0xff0000;
    
    private boolean showMilliseconds;
    private int defaultColor;
    
    public TimerLabel()
    {
        super();
        setAlignment(CENTER);
        
        showMilliseconds = false;
        defaultColor = getUnselectedStyle().getFgColor();
    }
    
    public void setTime(int milliseconds)
    {    
        setColor(milliseconds >= ZERO_TIME);
        setText(convertTimeToString(milliseconds));
        repaint();
    }

    public boolean isShowMilliseconds() 
    {
        return showMilliseconds;
    }

    public void setShowMilliseconds(boolean showMilliseconds) 
    {
        this.showMilliseconds = showMilliseconds;
    }
    
    public String getUIID()
    {
        return "TimerLabel";
    }
    
    private String convertTimeToString(int milliseconds)
    {        
        StringBuffer sb = new StringBuffer();
        int tmp = milliseconds / (60 * 1000);
        sb.append((tmp >= 10 ? "" : "0") + Integer.toString(tmp) + ":");
        milliseconds %= (60 * 1000);
        tmp = milliseconds / 1000;
        sb.append((tmp >= 10 ? "" : "0") + Integer.toString(tmp));
        if (showMilliseconds)
        {
            milliseconds %= 1000;
            sb.append(":" + (milliseconds < 10 ? "00" : (milliseconds < 100 ? "0" : "")) + Integer.toString(milliseconds));
        }
        
        return sb.toString();
    }
    
    private void setColor(boolean isZero)
    {
        if (isZero)
        {
            getUnselectedStyle().setFgColor(ZERO_COLOR);
        }
        else
        {
            getUnselectedStyle().setFgColor(defaultColor);
        }
    }
}
